package com.hjimi.ski.view.widget;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StyleRes;

import com.hjimi.ski.R;
import com.hjimi.ski.utils.Mutils;


/**
 * Created by Administrator on 2019/1/31 0031.
 */

public class BaseDialog extends Dialog {
    public AlertController mAlert;

    public BaseDialog(@NonNull Context context) {
        super(context);
    }

    public BaseDialog(@NonNull Context context, @StyleRes int themeResId) {
        super(context, themeResId);
        mAlert = new AlertController(this,getWindow());
    }

    protected BaseDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    /**
     * 设置文本
     * @param viewId
     * @param charSequence
     */
    public void setText(int viewId, CharSequence charSequence) {
        mAlert.setText(viewId,charSequence);
    }

    /**
     * 设置点击事件
     * @param viewId
     * @param listener
     */
    public void setOnClickListener(int viewId, View.OnClickListener listener) {
        mAlert.setOnClickListener(viewId,listener);
    }

    public <T extends View> T getView(int viewId) {
        return mAlert.getView(viewId);
    }

    public static class Builder{
        private AlertController.AlertParams P;
        public Builder(Context context) {
            this(context, R.layout.dialog_base);
        }

        public Builder(Context context, int themeResId) {
            P = new AlertController.AlertParams(context, themeResId);
        }
        //设置布局
        public Builder setContentView(View view){
            P.mView = view;
            P.mViewLayoutResId = 0;
            return this;
        }
        //设置布局内容的layoutId
        public Builder setContentView(int layoutId){
            P.mView = null ;
            P.mViewLayoutResId = layoutId;
            return this;
        }

        //设置文本
        public Builder setText(int viewId, CharSequence text){
            P.mTextArray.put(viewId,text);
            return this;
        }

        //设置点击事件
        public Builder setOnClickListener(int viewId, View.OnClickListener listener){
            P.mClickArray.put(viewId,listener);
            return this;
        }

        //设置宽度为全屏
        public Builder fullWidth(){
            P.mWidth = ViewGroup.LayoutParams.MATCH_PARENT;
            return this;
        }

        /**
         * 从底部弹出 是否有动画
         * @return
         */
        public Builder formBotton(boolean isAnimation){
            if(isAnimation){
                P.mAnimation = R.style.dialog_from_buttom_anim;
            }
            P.mGravity = Gravity.BOTTOM;
            return this;
        }

        /**
         * 设置dialog的宽
         * @param width
         * @return
         */
        public Builder setWidth(int width){
            P.mWidth = Mutils.dp2px(P.mContext,width);
            return this;
        }

        /**
         * 设置dialog的高
         * @param height
         * @return
         */
        public Builder setHeight(int height){
            P.mHeight = Mutils.dp2px(P.mContext,height);
            return this;
        }

        /**
         * 设置dialog的宽高
         * @param width
         * @param height
         * @return
         */
        public Builder setWidthAndHeight(int width, int height){
            P.mWidth = Mutils.dp2px(P.mContext,width);
            P.mHeight = Mutils.dp2px(P.mContext,height);
            return this;
        }

        /**
         * 添加默认动画
         * @return
         */
        public Builder addDefaultAnimation(){
            P.mAnimation = R.style.dialog_scale_anim;
            return this;
        }

        /**
         * 设置是否可以取消
         * @param isCancenable
         * @return
         */
        public Builder setCancelable(boolean isCancenable){
            P.mCancelable = isCancenable;
            return this;
        }

        /**
         * 添加动画
         * @return
         */
        public Builder setAnimations(int styleAnimation){
            P.mAnimation = styleAnimation;
            return this;
        }

        public BaseDialog create() {
            final BaseDialog dialog = new BaseDialog(P.mContext, P.mThemeResId);
            P.apply(dialog.mAlert);
            dialog.setCancelable(P.mCancelable);
            if (P.mCancelable) {
                dialog.setCanceledOnTouchOutside(true);
            }
            dialog.setOnCancelListener(P.mOnCancelListener);
            dialog.setOnDismissListener(P.mOnDismissListener);
            if (P.mOnKeyListener != null) {
                dialog.setOnKeyListener(P.mOnKeyListener);
            }
            return dialog;
        }

        public BaseDialog show() {
            final BaseDialog dialog = create();
            dialog.show();
            return dialog;
        }
    }
}
